﻿package com.pool.core
{
	import com.pool.data.Common;
	import com.pool.data.UserData;
	import com.pool.data.UserDataMgr;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import com.pool.events.LoginEvent;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import org.dm.display.DisplayMgr;
	
	import org.dm.general.ClassGather;
	
	import SpLib.Com.SpLibEvent;
	import SpLib.Net.NetMsgMgr;
	import SpLib.Net.RPC;
	/**
	 * ...
	 * @author Scott
	 */
	public class Login extends EventDispatcher
	{
		//主场景
		private var _display:Sprite;
		//内部元素
		private var _userName:TextField;
		private var _userPwd:TextField;
		private var _submit:SimpleButton;
		private var _register:SimpleButton;
		private var _gloadRing:Sprite;
		private var _loginBall:Sprite;
		
		
		private var numOfParticles:uint;
		private var particles:Array;
		private var container:Sprite;
		
		
		private var joss:Sprite;
		
		public function Login() 
		{
			super();
			
			_display = DisplayMgr.getDisplay("login.bg", "win.LoginWin");
			
			_userName = _display.getChildByName("userName") as TextField;
			_userPwd = _display.getChildByName("userPwd") as TextField;
			_submit = _display.getChildByName("submit") as SimpleButton;
			_register = _display.getChildByName("register") as SimpleButton;
			_gloadRing = _display.getChildByName("GlodRing") as Sprite;
			_loginBall = _display.getChildByName("LoginBall") as Sprite;
			
			init();
		}
		
		private function init():void 
		{
			_userName.text = "forgame01";
			_userPwd.text = "1111";
			_submit.addEventListener(MouseEvent.CLICK, onLogin);
			_register.addEventListener(MouseEvent.CLICK, onRegister);			
			_display.addEventListener(Event.ENTER_FRAME, makeParticles);
		}   
		
		public function get display():Sprite
		{
			return this._display; 
		}
		
		public function stopAll():void 
		{
			_display.removeEventListener(Event.ENTER_FRAME, makeParticles);
		}
		
		public function onLogin(e:MouseEvent):void 
		{
			var _clipEvent:LoginEvent=new LoginEvent(LoginEvent.LOGIN);
			_clipEvent.userName = _userName.text;
			_clipEvent.userPwd = _userPwd.text;
			dispatchEvent(_clipEvent);
		}
		
		public function onRegister(e:MouseEvent):void 
		{
			trace("eafe");
		}
		
		public function checkUser():void 
		{
			if(!SpLib.Net.NetMsgMgr.IsConnected())
			{
				//var socket:SpLib.Net.Socket = SpLib.Net.NetMsgMgr.Connect(Common.serverIP[0],Common.serverIP[1]).addEventListener;
				SpLib.Net.NetMsgMgr.Connect(Common.serverIP[0],Common.serverIP[1]).addEventListener(SpLibEvent.NET_CONNECTED,OnConnectOk);
			}
			else
			{
				OnConnectOk(new SpLibEvent(SpLibEvent.NET_CONNECTED));
			}
		}
		
		private function OnConnectOk(e:SpLibEvent):void 
		{
			Common.Call("人员管理", "Login", [_userName.text, _userPwd.text, Common.gameSite, 1, Common.cltVer, ""], onLoginOK, onLoginErr);
		}
		
		private function onLoginOK(e:SpLibEvent):void 
		{
			UserDataMgr.Instance.LoginOk(int(e.m_Agv[0]), int(e.m_Agv[1]));
			dispatchEvent(new LoginEvent(SpLibEvent.RPC_RETOK));
		}
		
		private function onLoginErr(e:SpLibEvent):void 
		{
			dispatchEvent(new LoginEvent(SpLibEvent.RPC_RETERR));
		}
		
		public function getUserData():void
		{
			Common.Call("人员管理", "LoadData",[UserDataMgr.meAID,UserDataMgr.meLoginKey,],onGetDataOK,onGetDataErr);
		}
		
		public function onGetDataOK(e:SpLibEvent):void
		{
			UserDataMgr.Instance.LoadDataOk(e.m_Agv[0], e.m_Agv[1]);
			dispatchEvent(new LoginEvent(LoginEvent.GETDATAOK));
		}
		
		public function onGetDataErr(e:SpLibEvent):void
		{
			trace(e.m_sErrMsg);
		}
		
		private function cleanAll():void 
		{
			_userName.text = "";
			_userPwd.text = "";
		}
		
		public function makeParticles(event:Event):void
		{
			_gloadRing.rotation+=.5
		}
	}

}